
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <script>

//    function deeCopy(obj){
//        let map = new WeakMap();
//        function dp(obj){
//             let result = null;
//              let keys = Object.keys(obj);
//              let  key = null;
//            for (let i = 0; i < keys.length; i++) {
//              if (condition) {
                 
//              }
               
//            }
//        }
//    }

  let obj1={
      a:{a:1,b: '小明'},
      say(){
      
      },
      res: undefined,
      string:'小明',
      result: null,
      
  }
   
  let obj2 = JSON.parse(JSON.stringify(obj1));
  console.log(obj2); //函数和ub不能调用
  
  let obj4 = obj1.a.b= '小红';
  console.log(obj1);
  let obj3 = Object.assign(obj1);
  console.log(obj3); //只能实现单层拷贝


  //递归函数
  function deepCopy(obj){
      let result = obj;
      if (typeof obj === 'object'&& obj !== null) {
          result = Object.prototype.toString.call(obj)==='[object Object]'?{}:[]
        for (let prop in obj) {
            result[prop] = deepCopy(obj[prop])
            
        }

      }
      return result
  }

  function deepCopy2(obj){
      let result = obj;
      if ( typeof obj === 'object'&& obj!== null) {
          result = Object.prototype.toString.call(obj)==='[object,Object]'?{}:[]
        for(let prop in obj){
            result[prop] = deepCopy(obj[prop])
        }

    }
    return result
  }


// JSON.parse(JSON.stringify(obj))
// Object.assign()
// let res = obj;
// if (typeof obj ==='object' && obj != null) {
//     res = Object.prototype.toString.call(obj)='[object,Object]'?{}:[]
// for(let prop in obj){
//     res[prop]=deepCopy(obj[prop])
// }

// }



  let obj5 = deepCopy2(obj1);
  console.log(obj5);




    </script>
    
</body>
</html>